package com.seckill.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.seckill.pojo.entity.SeckillCoupon;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 秒杀优惠券Mapper接口
 */
@Mapper
public interface SeckillCouponMapper extends BaseMapper<SeckillCoupon> {

    /**
     * 新增秒杀优惠券
     * @param seckillCoupon
     */
    @Insert("INSERT INTO seckill_coupon (name, type, value, `condition`, description, activity_id, stock, take_count, limit_count, valid_start_time, valid_end_time, create_time, update_time, create_user, update_user) " +
            "VALUES (#{name}, #{type}, #{value}, #{condition}, #{description}, #{activityId}, #{stock}, #{takeCount}, #{limitCount}, #{validStartTime}, #{validEndTime}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(SeckillCoupon seckillCoupon);

    /**
     * 根据活动id查询秒杀优惠券
     * @param activityId
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM seckill_coupon " +
            "<where>" +
            "<if test='activityId != null'>" +
            "activity_id = #{activityId}" +
            "</if>" +
            "</where>" +
            "ORDER BY create_time DESC" +
            "</script>")
    List<SeckillCoupon> pageQuery(@Param("activityId") Long activityId);

    /**
     * 根据id查询秒杀优惠券
     * @param id
     * @return
     */
    @Select("SELECT * FROM seckill_coupon WHERE id = #{id}")
    SeckillCoupon getById(Long id);

    /**
     * 修改秒杀优惠券
     * @param seckillCoupon
     */
    @Update("UPDATE seckill_coupon SET name = #{name}, type = #{type}, value = #{value}, `condition` = #{condition}, " +
            "description = #{description}, activity_id = #{activityId}, stock = #{stock}, " +
            "take_count = #{takeCount}, limit_count = #{limitCount}, valid_start_time = #{validStartTime}, " +
            "valid_end_time = #{validEndTime}, update_time = #{updateTime}, update_user = #{updateUser} " +
            "WHERE id = #{id}")
    void update(SeckillCoupon seckillCoupon);

    /**
     * 删除秒杀优惠券
     * @param id
     */
    @Delete("DELETE FROM seckill_coupon WHERE id = #{id}")
    void delete(Long id);

    /**
     * 扣减库存
     * @param id
     * @return
     */
    @Update("UPDATE seckill_coupon SET stock = stock - 1, take_count = take_count + 1 WHERE id = #{id} AND stock > 0")
    int reduceStock(Long id);

    /**
     * 查询用户已领取的优惠券数量
     * @param couponId
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM user_coupon WHERE coupon_id = #{couponId} AND user_id = #{userId}")
    int countUserCoupon(@Param("couponId") Long couponId, @Param("userId") Long userId);

    /**
     * 用户领取优惠券
     * @param couponId
     * @param userId
     */
    @Insert("INSERT INTO user_coupon (coupon_id, user_id, status, create_time) VALUES (#{couponId}, #{userId}, 0, NOW())")
    void userTakeCoupon(@Param("couponId") Long couponId, @Param("userId") Long userId);

    /**
     * 扣减库存
     * @param id
     * @return
     */
    @Update("UPDATE seckill_coupon SET stock = stock - 1 WHERE id = #{voucherOrder.getVoucherId()} AND stock > 0 ")
    Boolean decStock(Long id);
}